package com.example.bishe.controller.authentication;

import com.alibaba.fastjson.JSONObject;
import com.example.bishe.entity.Tag;
import com.example.bishe.toolclass.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @Author yws
 * @Date 2022/3/5 16:42
 *
 */
@RestController
@RequestMapping("/WebLogin")
public class WebAuthentication {
    @Resource
    ApiReturnDate apiReturnDate;
    @Resource
    JsonValue jsonValue;
    @Resource
    GitTonk gitTonk;
    @Resource
    LoginUser loginUser;
    @Resource
    BiaoQIan biaoQIan;

    @CrossOrigin
    @GetMapping("/web_apply")
    public HashMap web_apply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(request.getParameter("code")==null){
            String x = MyEnum.SAOMA_LOGIN.getValue() +
                    "appid=" + MyEnum.APPID.getValue() +
                    "&agentid=" + MyEnum.DENGLU_AGENTID.getValue() +
                    "&redirect_uri=" + MyEnum.HOST.getValue() +
                    "WebLogin/web_apply";
            System.out.println("请求到了");
            response.sendRedirect(x);
            return null;
        }
        else {
            System.out.println("转发到了");
            String access_token = gitTonk.AccessToken("dengluaccess_token");
            //根据access_token和code获取用户的id基本信息
            String Url = MyEnum.USER_IDENTITY.getValue() +
                    "access_token=" + access_token +
                    "&code=" + request.getParameter("code");
            JSONObject mydate1 = apiReturnDate.Mydate(Url);
            String userId = jsonValue.getValue(mydate1, "UserId");

            String access_token1 = gitTonk.AccessToken("useraccess_token");

            //根据userid获取用户信息
            String url = MyEnum.USER_CAOZUO.getValue()+"get?"+
                    "access_token="+access_token1+
                    "&userid="+userId;
            JSONObject mydate2 = apiReturnDate.Mydate(url);
            HashMap userxinxi = jsonValue.getValue(mydate2, "userid","gender","department","email");

            String userid = jsonValue.getValue(mydate2, "userid");
            loginUser.login(userid,"123456");
            List<Tag> list = biaoQIan.biaoQian();//获取到所有的标签信息
            //System.out.println(list);
            Map<String,List<TagUser>> tagUsers = biaoQIan.gitTagUser(list);
            Set<String> set = new HashSet();
            set = tagUsers.keySet();
            List<String> list1 = new ArrayList<>();
            for (String str:set) {//输出所有的标签和对应标签下的人员
                for (TagUser x:tagUsers.get(str)){
                    if(x.getUserid().equals(userxinxi.get("userid")))
                        list1.add(str);
                }
            }
            userxinxi.put("tagName",list1);
            return userxinxi;
        }
    }

    @CrossOrigin
    @GetMapping("/jieshou")
    public HashMap jieshou(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /**
         *获取到access_tonk
         **/
        return null;
    }

    @CrossOrigin
    @GetMapping("/login")
    public HashMap login(String userId){
        HashMap userxinxi = new HashMap();
        loginUser.login(userId,"123456");
        List<Tag> list = biaoQIan.biaoQian();//获取到所有的标签信息
        //System.out.println(list);
        Map<String,List<TagUser>> tagUsers = biaoQIan.gitTagUser(list);
        Set<String> set = new HashSet();
        set = tagUsers.keySet();
        List<String> list1 = new ArrayList<>();
        for (String str:set) {//输出所有的标签和对应标签下的人员
            for (TagUser x:tagUsers.get(str)){
                if(x.getUserid().equals(userxinxi.get("userid")))
                    list1.add(str);
            }
        }
        userxinxi.put("login","登录成功");
        userxinxi.put("userid",userId);
        userxinxi.put("tagName",list1);
        return userxinxi;
    }
}